using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;

[Serializable]
public class MineMap
{
    public int HorizontalCount
    {
        get { return m_HorizontalCount; }
    }
    public int VerticalCount
    {
        get { return m_VerticalCount; }
    }
    public MineMap(int x, int y, int mineCount)
    {
        m_HorizontalCount = x;
        m_VerticalCount = y;
        m_MineCount = mineCount;
        m_Map = new int[x, y];

        UnityEngine.Random.InitState((int)System.DateTime.Now.Ticks);
        Generate();
    }

    public int GetItemValue(int x, int y)
    {
        return m_Map[x, y];
    }

    public int GetItemValue(Vector2 coord)
    {
        return m_Map[(int)coord.x, (int)coord.y];
    }

    public bool IsMine(int x, int y)
    {
        if (x < 0 || x > m_HorizontalCount || y < 0 || y > m_VerticalCount)
        {
            return false;
        }

        return m_Map[x, y] == -1;
    }

    public override string ToString()
    {
        StringBuilder ret = new StringBuilder();
        ret.AppendFormat("{0},{1}|", m_HorizontalCount, m_VerticalCount);
        int[] mapArr = new int[m_HorizontalCount * m_VerticalCount];
        for (int i = 0; i < m_HorizontalCount; i++)
        {
            for (int j = 0; j < m_VerticalCount; j++)
            {
                int idx = j * m_HorizontalCount + i;
                mapArr[idx] = GetItemValue(i, j);
            }
        }
        ret.Append(JsonConvert.SerializeObject(mapArr));

        return ret.ToString();
    }

    //=========================================公私分界线==================================================

    [JsonProperty]
    private int m_MineCount;
    [JsonProperty]
    private int m_HorizontalCount;
    [JsonProperty]
    private int m_VerticalCount;
    [JsonProperty]
    private int[,] m_Map;
    private void Generate()
    {
        for (int i = 0; i < m_MineCount; i++)
        {
            int x = i / m_VerticalCount;
            int y = i % m_VerticalCount;
            m_Map[x, y] = -1;
        }

        for (int i = m_HorizontalCount * m_VerticalCount - 1; i >= 0; i--)
        {
            int x = i / m_VerticalCount;
            int y = i % m_VerticalCount;

            int randomNum = (int)(UnityEngine.Random.value * i);
            int xRand = randomNum / m_VerticalCount;
            int yRand = randomNum % m_VerticalCount;

            int temp = m_Map[x, y];
            m_Map[x, y] = m_Map[xRand, yRand];
            m_Map[xRand, yRand] = temp;
        }

        for (int i = 0; i <= m_HorizontalCount * m_VerticalCount - 1; i++)
        {
            int x = i / m_VerticalCount;
            int y = i % m_VerticalCount;

            if (IsMine(x, y))
            {
                CalcMineCount(x, y);
            }
        }
    }


    private void CalcMineCount(int x, int y)
    {
        if (x >= 1 && y >= 1 && IsMine(x - 1, y - 1) == false) { m_Map[x - 1, y - 1]++; } //  左下角
        if (x >= 1 && IsMine(x - 1, y) == false) { m_Map[x - 1, y]++; } //  左边
        if (x >= 1 && y <= m_VerticalCount - 2 && IsMine(x - 1, y + 1) == false) { m_Map[x - 1, y + 1]++; } //  左上角
        if (y <= m_VerticalCount - 2 && IsMine(x, y + 1) == false) { m_Map[x, y + 1]++; } //  上边
        if (x <= m_HorizontalCount - 2 && y <= m_VerticalCount - 2 && IsMine(x + 1, y + 1) == false) { m_Map[x + 1, y + 1]++; } //  右上角
        if (x <= m_HorizontalCount - 2 && IsMine(x + 1, y) == false) { m_Map[x + 1, y]++; } //  右边
        if (x <= m_HorizontalCount - 2 && y >= 1 && IsMine(x + 1, y - 1) == false) { m_Map[x + 1, y - 1]++; } //  右下角
        if (y >= 1 && IsMine(x, y - 1) == false) { m_Map[x, y - 1]++; } //  下边
    }

}
